/* Single Pole Recursive Lowpass Filter
 * Copyright (c) 2011
 * All rights reserved.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
/* Implementation of a single pole low-pass filter as per
 * http://www.dspguide.com/ch19/2.htm
 *
 * y[n] = a0*x[n] + a1*x[n-1] + b1*y[n-1]  (EQ 1)
 * 
 * a0 = 1-C
 * a1 = 0
 * b1 = C
 *
 * C = exp(-2*pi*fs/fc); fs: samplerate, fc: cutoff frequency
 *
 *
 * Placing a0, a1 and b1 into EQ 1 yields:
 *
 * y[n] = (1-C)*x[n] + C*y[n-1]
 *
 * This can be transformed:
 *
 * y[n] = x[n] - C*x[n] + C*y[n-1]
 * y[n] = x[n] + C*(y[n-1] - x[n])  (EQ 2)
 *
 * which lets us replace the original EQ 1 with an equivalent equation EQ 2
 * which unlike the two multiplication and one addition in EQ 1 only uses one
 * multiplication and two additions (which should in most cases be computational
 * more efficient).
 *
 * EQ 2 can now be implemented as the final filter.
 *
 * The following code will demonstrate an implementation in the
 * JesuSonic scripting language: http://www.cockos.com/jesusonic/
 * For which the documentation can be found at:
 * http://www.cockos.com/jesusonic/help.html
 */
desc: RC Lowpass
slider1:100<20,22000,1>Frequency (Hz)

@init
y0 = y1 = 0; // y[-1] is assumed to be 0

@slider
C = exp(-2*3.1415*slider1/srate);

@sample
spl0 = y0 = spl0 + C*(y0 - spl0);
spl1 = y1 = spl1 + C*(y1 - spl1);
